package com.etc.layui.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID;

import com.etc.entity.Users;
import com.etc.layui.service.UsersService;
import com.etc.layui.service.impl.UsersServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class LayUiservlet
 */
@WebServlet("/lay/list")
public class LayUiservlet extends HttpServlet {
	UsersService us=new UsersServiceImpl();
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LayUiservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		
		String op="";
		if(request.getParameter("op")!=null) {
			op=request.getParameter("op");
		}
		
		if("laypage".equals(op)) {
		dopage(request,response);
		}else if("del".equals(op)){
		dodel(request,response);
		}else if("edit".equals(op)) {
			doEdit(request,response);
		 
		
		}

	}
	protected void doEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		int userId=Integer.parseInt(request.getParameter("userId"));
		String userName=request.getParameter("userName");
		String userSex=request.getParameter("userSex");
		String userPwd=request.getParameter("userPwd");
		String userTel=request.getParameter("userTel");
		String userAddress=request.getParameter("userAddress");
		int userLevel=Integer.parseInt(request.getParameter("userLevel"));
		int userState=Integer.parseInt(request.getParameter("userState"));
		Users user=new Users(userId, userName, userPwd, userSex, userTel, userAddress, userLevel, userState);
		boolean flag=us.update(user);
		String msg="修改失败";
		if(flag){
		msg="修改成功";
		}
		System.out.println(msg);
		out.print(msg);
		out.close();
		
	}
	protected void dodel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		int userId=Integer.parseInt(request.getParameter("userId"));
		boolean flag=us.delUsersById(userId);
		String msg="删除失败";
		if(flag){
		msg="删除成功";
		}
		System.out.println(msg);
		out.print(msg);
		out.close();
		
	}
	protected void dopage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out=response.getWriter();
		int page=1;
		int limit=10;
		String content="";
		if(request.getParameter("page")!=null) {
			page=Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("limit")!=null) {
			limit=Integer.parseInt(request.getParameter("limit"));
		}
		if(request.getParameter("content")!=null) {
			content=request.getParameter("content");
		}
		
		PageData<Users> pd=us.getUsersByPage(page, limit, content);
		pd.setCode(0);
		pd.setMsg("ok");
		Gson gson=new Gson();
		String str=gson.toJson(pd);
		out.print(str);
		out.close();
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
